import { setLocalSchedule, cancelAllSchedule } from "./schedule.js";
import { delay } from "./server.js";
import { roomSay, allConfig } from "../service/index.js";
/**
 * 群定时任务，针对群
 * @param {*} that bot对象
 * @param {*} item 任务项
 */
async function setRoomTask(that, item, name) {
    try {
        let time = item.date;
        let room = await that.Room.find({ topic: item.roomName });
        if (!room) {
            console.log(`查找不到群：${item.roomName}，请检查群名是否正确`);
            return;
        }
        else {
            console.log(`群：“${item.roomName}”设置定时任务成功`);
            setLocalSchedule(time, async () => {
                for (let reply of item.contents) {
                    console.log('群定时任务开始发送，内容：', `${item.type === 1 ? reply.content : reply.url}`);
                    await delay(1000);
                    await roomSay(room, '', reply);
                }
            }, name);
        }
    }
    catch (error) {
        console.log('设置群定时任务失败：', error);
    }
}
/**
 * 每日说定时任务设定，针对好友
 * @param {*} that bot对象
 * @param {*} item 任务项
 */
async function setEveryDayTask(that, item, name) {
    try {
        let time = item.date;
        let contact = (await that.Contact.find({ alias: item.alias })) || (await that.Contact.find({ name: item.name })); // 获取你要发送的联系人
        if (!contact) {
            console.log(`查找不到用户昵称为'${item.name}'或备注为'${item.alias}'的用户，请检查设置用户是否正确`);
            return;
        }
        else {
            console.log(`设置用户：“${item.name}|${item.alias}”每日说任务成功`);
            setLocalSchedule(time, async () => {
                let content = await getEveryDayContent(item.memorialDay, item.city, item.endWord);
                console.log('每日说任务开始工作,发送内容：', content);
                await pushJob(async () => {
                    await contact.say(content);
                });
            }, name);
        }
    }
    catch (error) {
        console.log('每日说任务设置失败');
    }
}
/**
 * 设置定时任务
 * @param {*} that bot 对象
 * @param {*} item 定时任务项
 */
async function setScheduleTask(that, item, name) {
    let time = item.isLoop ? item.time : new Date(item.time);
    setLocalSchedule(time, async () => {
        try {
            let contact = await that.Contact.find({ name: item.subscribe });
            console.log(`${item.subscribe}的专属提醒开启啦！`);
            await pushJob(async () => {
                await contact.say(item.content);
            });
            if (!item.isLoop) {
                await updateSchedule(item.id);
            }
        }
        catch (error) {
            console.log('设置定时任务错误', error);
        }
    }, name);
}
/**
 * 初始化提醒任务
 * @param {}} that
 */
async function initTimeSchedule(that) {
    cancelAllSchedule('time_tips');
    let scheduleList = await getScheduleList(); // 获取定时任务
    if (scheduleList && scheduleList.length > 0) {
        for (let item of scheduleList) {
            setScheduleTask(that, item, `time_tips_${item.id}`);
        }
    }
}
/**
 * 初始化定时任务
 * @param {}} that
 */
async function initTaskLocalSchedule(that) {
    cancelAllSchedule('task');
    const config = await allConfig(); // 获取配置信息
    const { dayTaskSchedule, roomTaskSchedule } = config;
    if (dayTaskSchedule && dayTaskSchedule.length > 0) {
        dayTaskSchedule.forEach((item, index) => {
            setEveryDayTask(that, item, `task_day_${index}`);
        });
    }
    if (roomTaskSchedule && roomTaskSchedule.length > 0) {
        roomTaskSchedule.forEach((item, index) => {
            setRoomTask(that, item, `task_room_${index}`);
        });
    }
}
/**
 * 初始化小助手任务
 * @param {*} that bot对象
 * @param {*} scheduleList 提醒任务列表
 * @param {*} daySayList 每日说任务列表
 * @param {*} RoomSayList 群资讯任务列表
 */
async function initAllSchedule(that) {
    await initTimeSchedule(that);
    await initTaskLocalSchedule(that);
}
export { initTaskLocalSchedule };
export { initAllSchedule };
export { initTimeSchedule };
export default {
    initTaskLocalSchedule,
    initAllSchedule,
    initTimeSchedule
};
